{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 哈希查找\n",
    "\n",
    "\n",
    "### 算法简介\n",
    "\n",
    "       哈希表就是一种以键-值(key-indexed) 存储数据的结构，只要输入待查找的值即key，即可查找到其对应的值。\n",
    "\n",
    "### 算法思想\n",
    "\n",
    "    哈希的思路很简单，如果所有的键都是整数，那么就可以使用一个简单的无序数组来实现：将键作为索引，值即为其对应的值，这样就可以快速访问任意键的值。这是对于简单的键的情况，我们将其扩展到可以处理更加复杂的类型的键。\n",
    "\n",
    "### 算法流程\n",
    "\n",
    "      1）用给定的哈希函数构造哈希表；\n",
    "      2）根据选择的冲突处理方法解决地址冲突；\n",
    "         常见的解决冲突的方法：拉链法和线性探测法。\n",
    "      3）在哈希表的基础上执行哈希查找。\n",
    "### 复杂度分析\n",
    "\n",
    "      单纯论查找复杂度：对于无冲突的Hash表而言，查找复杂度为O(1)（注意，在查找之前我们需要构建相应的Hash表）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "class HashTable:\n",
    "    def __init__(self, size):\n",
    "        self.elem = [None for _ in range(size)]\n",
    "        self.size = size # 最大表长\n",
    "    \n",
    "    def hash(self, key):\n",
    "        return key % self.size # 散列函数采用除留余数法\n",
    "    \n",
    "    def insert_hash(self, key):\n",
    "        address = self.hash(key)\n",
    "        while self.elem[address]:\n",
    "            address = (address + 1) % self.size # 线性探测下一地址是否可用 \n",
    "        self.elem[address] = key  # 没有冲突则直接保存。\n",
    "    \n",
    "    def search_hash(self, key):\n",
    "        star = address = self.hash(key)\n",
    "        while self.elem[address] != key:\n",
    "            address = (address + 1) % self.size\n",
    "            if not self.elem[address] or address == star: # 说明没找到或者循环到了开始的位置\n",
    "                return False\n",
    "        return True\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(12, 0) (25, 1) (37, 2) (15, 3) (16, 4) (29, 5) (48, 6) (67, 7) (56, 8) (34, 9) (22, 10) (47, 11) \n",
      "\n",
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "list_a = [12, 67, 56, 16, 25, 37, 22, 29, 15, 47, 48, 34]\n",
    "hash_table = HashTable(len(list_a))\n",
    "for i in list_a:\n",
    "    hash_table.insert_hash(i)\n",
    "\n",
    "for i in hash_table.elem:\n",
    "    if i:\n",
    "        print((i, hash_table.elem.index(i)), end=\" \")\n",
    "print(\"\\n\")\n",
    "\n",
    "print(hash_table.search_hash(15))\n",
    "print(hash_table.search_hash(33))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 线性探测法有个问题，1、size取多大呢？ 2、容易产生，效率不高"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
